<template>
    <div class="file-item-container">
        <img class="file-icon" :src="fileType(data.materialType)" alt="">
        <div :class="['file-content', {'file-content2': type === 'download'}]">
            <p class="file-name"><Ellipsis :content="data.materialName || ''" :line-height="14"></Ellipsis></p>
            <p class="file-size">{{data.materialSizeFormat}}</p>
        </div>
        <div class="mar-l20">
            <div v-if="type === 'download'" class="flex-center">
                <el-button class="option-btn mar-r12" @click="viewFile">预览</el-button>
                <fileDownload :material="data" :tooltipDisabled="true" downloadClass="option-btn"></fileDownload>
            </div>
            <template v-else-if="type === 'delete'">
                <el-button class="option-btn option-btn2" @click="onDelete">
                    <i class="iconfont icon-shanchu delete-icon"></i>
                </el-button>
            </template>
        </div>
        <ContentPreviewDialog v-model="fileModelShow" :form="fileData"/>
    </div>
</template>

<script setup>
import fileDownload from '@/components/file-download.vue';
import Ellipsis from '@/components/ellipsis.vue';
import ContentPreviewDialog from '@/components/content-preview-dialog.vue';
import { fileType } from '@/lib/tools.js';
import { ref } from 'vue';

const emit = defineEmits(['no-permission', 'on-delete']);

const props = defineProps({
    data: {
        type: Object,
        default: () => {}
    },
    type: { // download 下载 delete 删除
        type: String,
        default: 'download'
    }
});

function onDelete () {
    emit('on-delete', props.data.materialId)
}

let fileModelShow = ref(false); // 文件预览弹窗是否显示

let fileData = ref({});

// 查看文件
function viewFile () {
    fileData.value.title = props?.data?.materialName;
    fileData.value.materialVO = props?.data?.materialVo;
    fileModelShow.value = true;
}
</script>

<style lang="scss" scoped>
.file-item-container {
    width: 100%;
    height: 66px;
    background: #F7F9FA;
    border-radius: 4px;
    padding: 12px 12px 14px 12px;
    display: flex;
    box-sizing: border-box;
    .file-icon {
        width: 22px;
        height: 22px;
        margin-right: 3px;
    }
    .file-content {
        padding-top: 4px;
        width: calc(100% - 75px);
        flex: 1;
        line-height: 14px;
        .file-size {
            margin-top: 10px;
            color: var(--srt-color-gray);
        }
    }
    .file-content2 {
        width: calc(100% - 130px);
    }

    .mar-l20 {
        // width: 50px;
        padding-left: 20px;
        text-align: right;
        box-sizing: border-box;
    }
    .mar-r12 {
        margin-right: 12px;
    }
    :deep(.option-btn) {
        font-size: 14px;
        white-space: nowrap;
        // height: 14px;
        min-height: 14px;
        padding: 0;
        padding-right: 8px;
        color: var(--srt-main-active-text-color);
        border: none;
        background-color: transparent;
        &:hover {
            background-color: transparent;
        }
    }
    .option-btn2 {
        margin-top: -13px;
        padding-right: 0;
        .delete-icon {
            color: var(--srt-color-light-gray);
        }
        &:hover {
            .delete-icon {
                color: var(--srt-main-active-text-color);
            }
        }
    }
    .flex-center {
        height: 100%;
        display: flex;
        align-items: center;
    }
}
</style>